﻿/*************************   Auto Generated Repository Code For Client table     Generated @ 4/15/2013 4:25:00 PM *************************/


using System;
using System.Collections.Generic;
using System.Linq;
using BusinessObjects;

namespace DataLayer
{
    public partial class ClientRepository
    {
        private PracticeSimpleDataContext dB;


        /// <summary>
        /// Retrieve an Client by its ClientID (Primary Key)
        /// </summary>
        /// <param name="ClientID">The ID of the <see cref="Client"/> to retrieve</param>
        /// <returns>A <see cref="Client"/> object</returns>
        public Client GetClient(int ClientID)
        {
            Client retObject;

            using (dB = new PracticeSimpleDataContext())
            {
                retObject = (from x in dB.Clients where x.ClientID == ClientID select x).SingleOrDefault();
            }

            return retObject;
        }

        /// <summary>
        /// Return a list of Client objects
        /// </summary>
        /// <returns>A List of type <see cref="Client"/></returns>
        public IList<Client> GetClientsList()
        {
            IList<Client> retObject;
            using (dB = new PracticeSimpleDataContext())
            {
                retObject = (from x in dB.Clients select x).ToList();
            }
            return retObject;
        }


        /// <summary>
        /// Accepts a Client Business object, converts it and saves it to the database
        /// </summary>
        /// <returns>Void</returns>
        public void Save(BusinessObjects.Client businessObject)
        {
            using (dB = new PracticeSimpleDataContext())
            {
                dB.Clients.InsertOnSubmit(ConvertToDataObject(businessObject));
                dB.SubmitChanges();
            }
        }

        /// <summary>
        /// Accepts the Primary Key of a Client, finds it in the database and deletes it
        /// </summary>
        /// <param name="ClientID">The ID of the <see cref="Client"/> to retrieve</param>
        /// <returns>Void</returns>
        public void Delete(int ClientID)
        {
            using (dB = new PracticeSimpleDataContext())
            {
                DataLayer.Client obj = (from x in dB.Clients where x.ClientID == ClientID select x).SingleOrDefault();
                dB.Clients.DeleteOnSubmit(obj);
                dB.SubmitChanges();
            }
        }

        /// <summary>
        /// Converts a Client data object to a Client business object
        /// </summary>
        /// <returns>A business object of type <see cref="Client"/></returns>
        public BusinessObjects.Client ConvertToBusinessObject(DataLayer.Client dataObject)
        {
            BusinessObjects.Client businessObject = new BusinessObjects.Client();

            businessObject.ClientID = dataObject.ClientID;
            businessObject.ClientName = dataObject.ClientName;


            return businessObject;

        }

        /// <summary>
        /// Converts a Client business object to a Client data object
        /// </summary>
        /// <returns>A data object of type <see cref="Client"/></returns>
        public DataLayer.Client ConvertToDataObject(BusinessObjects.Client businessObject)
        {
            DataLayer.Client dataObject = new DataLayer.Client();

            dataObject.ClientID = businessObject.ClientID;
            dataObject.ClientName = businessObject.ClientName;


            return dataObject;
        }
    }
}


